﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Assets.Script
{
    public class MainPanelControl
    {
        MainPanelView view;
        MainPanelModel model;
        List<AdditionSprite> additionSprites = new List<AdditionSprite>();
        List<AdvertisingSprite> advertingSprites = new List<AdvertisingSprite>();
        int nowLevel = 1;
        public MainPanelControl(MainPanelView mainPanelView)
        {
            MessageCenter.Ins.ListenMess<CurrencyData>(MessId.RefureCurrencyDataText, RefureCurrencyDataText);
            MessageCenter.Ins.ListenMess<int>(MessId.RefureNowPlayerLevelData, RefureNowPlayerLevelData);
            MessageCenter.Ins.ListenMess<PropType>(MessId.ConcealAdvertising, ConcealAdvertising);
            view = mainPanelView;
            model = new MainPanelModel("MainPanelModelName");
            BaindEvent();
            MessageCenter.Ins.BuredMess(MessId.RefureCurrencyData, new CurrencyData
            {
                zuanshi = 30,
                gold = 10000
            });  
            MessageCenter.Ins.BuredMess(MessId.RefureNowPlayerLevelData,0);
        }
        

        private void ConcealAdvertising(PropType type)
        {
            if (type == PropType.nullData)
            {
                for (int i = 0; i < advertingSprites.Count; i++)
                {
                    advertingSprites[i].gameObject.SetActive(true);
                }
            }
            else
            {
                for (int i = 0; i < advertingSprites.Count; i++)
                {
                    if (advertingSprites[i].m_data.type == type)
                    {
                        advertingSprites[i].gameObject.SetActive(false);
                    }
                }
            }
        }

        private void RefureNowPlayerLevelData(int obj)
        {
            nowLevel += obj;
            view.levelText.text = "Lv" + nowLevel;
            model.RefurePlayerData(new PlayerData
            {
                nowLevel = nowLevel
            });
        }

        private void RefureCurrencyDataText(CurrencyData data)
        {
            view.zuanText.text = ShowCurrencyNum(data.zuanshi);
            view.goldText.text = ShowCurrencyNum(data.gold);
            for (int i = 0; i < additionSprites.Count; i++)
            {
                if (additionSprites[i].nowReleverGold > data.gold)
                {
                    additionSprites[i].RefureIsBuy(false);
                }
                else
                {
                    additionSprites[i].RefureIsBuy(true);
                }
            }
        }

        private string ShowCurrencyNum(int gold)
        {
            if (gold < 0)
                return "-" + ShowCurrencyNum(-gold);

            if (gold < 1000)
            {
                return gold.ToString();
            }
            else if (gold < 1_000_000)
            {
                return (gold / 1000.0).ToString("0.#") + "k";
            }
            else
            {
                return (gold / 1_000_000.0).ToString("0.#") + "m";
            }
        }

        private void BaindEvent()
        {
            view.tapToStartBtn.onClick.AddListener(() =>
            {
                UIManager.Ins.CloseUI(UIName.MainPanel);
                SceneManager.LoadScene("GameScenes");
                SceneManager.sceneLoaded += (s, b) =>
                {
                    if(SceneManager.GetActiveScene().name== "GameScenes")
                    UIManager.Ins.OpenUI(UIName.GamePanel);
                    
                };
                
                Debug.Log(model.GetPlayerData().nowLevel + "\r\n" + model.GetPlayerData().addSpeedTime + "\r\n" + model.GetPlayerData().magnetTime);
            });
            view.installBtn.onClick.AddListener(() =>
            {
                UIManager.Ins.OpenUI(UIName.InstallPanel);
            });
            view.skinBtn.onClick.AddListener(() =>
            {
                UIManager.Ins.OpenUI(UIName.SkinPanel);
            });
            for (int i = 0; i < GameDataSprite.Ins.advertisingDatas.Count; i++)
            {
                GameObject obj = GameObject.Instantiate(view.advertisingIcon, view.advertisingTran);
                obj.GetComponent<AdvertisingSprite>().RefureData(GameDataSprite.Ins.advertisingDatas[i]);
                advertingSprites.Add(obj.GetComponent<AdvertisingSprite>());
            }
            for (int i = 0; i < GameDataSprite.Ins.additionDatas.Count; i++)
            {
                GameObject obj = GameObject.Instantiate(view.additionIcon, view.additionTran);
                obj.GetComponent<AdditionSprite>().RefureData(GameDataSprite.Ins.additionDatas[i]);
                additionSprites.Add(obj.GetComponent<AdditionSprite>());
            }
            view.nameText.text = "Player";
        }

        internal void Open()
        {
            ConcealAdvertising(PropType.nullData);
        }
    }
}
